package comHttpsession;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

@WebServlet(name = "two14Servlet")
public class two14Servlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session=request.getSession();
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out=response.getWriter();
        Enumeration enumeration =session.getAttributeNames();
        ServletContext servletContext=request.getServletContext();
        out.write("<table align=\"center\" border=\"\">");
        out.write("<tr><td>"+"商品名"+"</td><td>"+"数量"+"</td></tr>");
        while (enumeration.hasMoreElements())
        {
            String name= (String) enumeration.nextElement();
            int num= (int) session.getAttribute(name);
            out.write("<tr><td>"+name+"</td><td>"+num+"</td></tr>");
        }
        out.write("<tr><td>"+"总价：</td><td>"+servletContext.getAttribute("money")+"</td></tr>");
        out.write("</table>");
    }
}
